Load packages

If you are using the package for the first time, you will have to first install it

# install.packages("survival") 
library(survival)
## Warning: package 'survival' was built under R version 4.0.4

Get data

Load data sets from package

pbc <- survival::pbc
pbcseq <- survival::pbcseq

Cross-sectional data

apply

Obtain the mean of columns time and age in the pbc data set

apply(pbc[, c(2,5)], 2, mean)
##       time        age 
## 1917.78230   50.74155
apply(pbc[, c("time", "age")], 2, mean)
##       time        age 
## 1917.78230   50.74155

Obtain the mean of rows in the pbc data set
Before running the code, think if it is meaningful

# apply(pbc[, ], 1, mean)

Obtain the standardized values of columns time, age and bili in the pbc data set

apply(pbc[, c("time", "age", "bili")], 2, function(x) { (x-mean(x))/sd(x) } )
##                time          age         bili
##   [1,] -1.373965243  0.768020788  2.559085710
##   [2,]  2.337540359  0.546051607 -0.481182150
##   [3,] -0.819955139  1.850349850 -0.413116451
##   [4,]  0.006533792  0.382785020 -0.322362187
##   [5,] -0.374574466 -1.209522821  0.040654871
##   [6,]  0.529765557  1.485293099 -0.549247848
##   [7,] -0.077654018  0.458783913 -0.503870716
##   [8,]  0.496271482  0.221614954 -0.662690679
##   [9,]  0.436525295 -0.788122063 -0.004722261
##  [10,] -1.689895842  1.896997447  2.128002953
##  [11,]  1.669469351  0.284510589 -0.413116451
##  [12,] -1.460868789  0.803661648  0.086032003
##  [13,]  1.501998976 -0.483602360 -0.571936414
##  [14,] -0.634379859  0.524562265 -0.549247848
##  [15,]  1.508335693  1.330936726 -0.549247848
##  [16,]  1.587997277 -0.985719184 -0.571936414
##  [17,] -1.039929739  0.138016171 -0.118165092
##  [18,] -1.617476221  0.305213736  1.855740160
##  [19,]  2.094934627 -0.113042241 -0.571936414
##  [20,] -0.508550766  0.881757062  0.426360495
##  [21,]  1.382506600  1.287171847 -0.594624980
##  [22,] -1.126833285  0.529803568  0.040654871
##  [23,] -1.497078600  0.500190206  3.217054127
##  [24,]  1.956432101 -0.595504179 -0.254296489
##  [25,]  1.999883874 -0.542567019 -0.571936414
##  [26,] -0.428889183  0.122816393  0.449049061
##  [27,] -1.666359465  0.353957854  4.169973904
##  [28,] -1.239083698 -0.554622015  3.171676995
##  [29,]  2.345687567  1.257296420 -0.571936414
##  [30,] -1.445479619 -0.895568773  0.086032003
##  [31,]  1.739173236 -0.879582799  0.335606230
##  [32,]  2.358361000  0.311503300 -0.322362187
##  [33,]  1.133564152  0.051796738 -0.549247848
##  [34,]  1.824266292  0.126223240 -0.549247848
##  [35,]  0.841169930 -0.203192652 -0.458493584
##  [36,]  1.532777315  0.542644760 -0.662690679
##  [37,] -1.534193656  1.051575278  0.880131817
##  [38,]  1.200552301 -1.351038001  0.017966305
##  [39,]  0.343285032  0.445156525 -0.571936414
##  [40,]  2.307667265 -0.389783037 -0.435805018
##  [41,] -0.513982238 -1.637475208  0.812066119
##  [42,]  2.294993831 -1.631709775 -0.254296489
##  [43,]  2.388234094 -0.179082658 -0.481182150
##  [44,]  1.367117431 -1.259577264  0.017966305
##  [45,]  1.907548856 -0.856521066 -0.594624980
##  [46,]  0.306169976 -0.473119754  0.562491892
##  [47,]  0.595848462 -0.317190991 -0.617313546
##  [48,]  2.271457454 -0.153662339 -0.299673621
##  [49,] -1.095149700  0.996541597 -0.549247848
##  [50,]  0.615763858  0.264855703 -0.481182150
##  [51,]  1.751846670  0.128843891 -0.549247848
##  [52,]  0.423851861 -0.019222917  0.630557590
##  [53,] -0.830818082  1.595360461 -0.140853658
##  [54,] -0.437941635 -1.104958827 -0.435805018
##  [55,] -0.504929785  1.437859307 -0.322362187
##  [56,] -0.064075339 -1.639047599 -0.481182150
##  [57,]  1.234951622  0.270883202 -0.208919356
##  [58,]  2.300425303 -0.590787006 -0.571936414
##  [59,]  0.277202127 -0.990436357 -0.549247848
##  [60,]  2.215332248  0.731331667 -0.526559282
##  [61,]  2.116660514 -0.654992967 -0.594624980
##  [62,]  1.061144530  0.953824978 -0.435805018
##  [63,] -0.958457665 -0.393714014  4.374170999
##  [64,] -0.389963636  1.164525357 -0.254296489
##  [65,]  1.877675763 -1.008780917 -0.458493584
##  [66,]  2.057819571 -0.410486184 -0.413116451
##  [67,]  0.770560799  0.052320868 -0.481182150
##  [68,]  1.920222290 -1.735225508 -0.571936414
##  [69,] -0.676926386 -0.134269518  3.806956846
##  [70,]  1.394274789  0.541596500 -0.594624980
##  [71,]  2.062345797 -0.181441244 -0.458493584
##  [72,]  2.051482854 -1.746756375 -0.617313546
##  [73,]  2.056914326 -1.172309570 -0.571936414
##  [74,] -0.082180245  0.112857917  1.175083177
##  [75,] -0.657916236 -0.691420023  3.148988429
##  [76,] -1.671790937  0.114954438  2.037248689
##  [77,] -1.440953393 -0.087621921  0.766688987
##  [78,] -0.206198846 -0.267660678  0.698623288
##  [79,]  1.619680861 -0.404458685 -0.549247848
##  [80,] -0.930395061  1.595622526  0.902820383
##  [81,]  0.563259632  1.198593827  2.536397144
##  [82,]  1.499283240  1.585926116  0.290229098
##  [83,]  1.930179988  0.504645314 -0.435805018
##  [84,]  1.913885573  0.487086949 -0.640002113
##  [85,]  1.303750262 -0.337370007 -0.254296489
##  [86,] -0.236071940  0.193049853  0.403671929
##  [87,] -1.556824788 -1.288666496 -0.481182150
##  [88,]  0.483598048 -0.894782578 -0.594624980
##  [89,] -0.160031338  0.162912361 -0.276985055
##  [90,]  0.698141177 -1.652674987 -0.367739319
##  [91,] -1.319650527 -0.491464315  0.403671929
##  [92,] -1.384828186  2.485595770 -0.413116451
##  [93,]  1.806161386 -1.359948216 -0.435805018
##  [94,] -1.057129399  0.303903410 -0.004722261
##  [95,] -1.618381466 -0.416513682  3.217054127
##  [96,]  1.749130934 -0.181441244 -0.503870716
##  [97,] -1.182958491  2.024623174 -0.276985055
##  [98,]  1.724689312 -2.092158240 -0.503870716
##  [99,]  1.721973576 -0.217606235 -0.322362187
## [100,] -1.236367962  0.069617168 -0.208919356
## [101,]  1.505619957 -0.554359950 -0.526559282
## [102,]  1.069291737  0.557844539 -0.526559282
## [103,] -1.636486371 -0.170172443 -0.163542224
## [104,]  1.057523549 -0.739377945 -0.481182150
## [105,]  1.062955021 -1.598689566 -0.481182150
## [106,]  1.180636906  1.700710651 -0.254296489
## [107,]  1.330907620  1.127574171 -0.594624980
## [108,]  0.602185179 -0.036781282 -0.640002113
## [109,]  0.530670802 -0.639269058 -0.617313546
## [110,]  0.169477940 -1.132475667 -0.299673621
## [111,]  0.391263031 -0.917844311  0.517114759
## [112,]  1.382506600  0.451446089 -0.276985055
## [113,] -0.848922987  0.047079565  0.789377553
## [114,]  1.337244337  0.199601481 -0.004722261
## [115,]  1.361685959 -0.775542935 -0.571936414
## [116,]  1.283834866  0.988679643 -0.050099394
## [117,] -0.755682725 -0.103869960  0.744000420
## [118,]  0.335137824 -0.180655049  0.063343437
## [119,] -1.269862037  0.336399489 -0.594624980
## [120,]  0.104300281 -1.492291116  0.063343437
## [121,] -1.563161504  1.643056318 -0.435805018
## [122,]  1.248530301  0.449349568 -0.594624980
## [123,] -0.857070195 -0.471023233  0.426360495
## [124,]  1.042134379  0.205628980 -0.594624980
## [125,]  0.498081973 -0.340776854 -0.435805018
## [126,] -0.990141249  0.273503853 -0.458493584
## [127,]  1.210509999 -0.635338081 -0.617313546
## [128,] -0.797324007 -0.841583353  2.944791334
## [129,]  1.196026075  1.232138166 -0.526559282
## [130,] -0.456951786 -0.623545149  3.217054127
## [131,] -0.966604872  1.077781793 -0.095476526
## [132,]  0.928978721 -0.975236578 -0.299673621
## [133,]  0.795002421  1.139367103 -0.390427885
## [134,]  1.114554001 -0.804632167 -0.571936414
## [135,]  1.115459246 -0.744095118 -0.640002113
## [136,]  1.068386492  0.499666076 -0.549247848
## [137,]  0.970620003  1.160070250 -0.481182150
## [138,] -0.561960237  0.048651956  0.925508949
## [139,]  0.170383185 -0.380872822 -0.481182150
## [140,]  1.033081927  0.319103189 -0.481182150
## [141,]  1.024934719 -0.354666307 -0.526559282
## [142,]  0.453724955  0.477128473 -0.503870716
## [143,] -1.024540569 -0.444030523 -0.072787960
## [144,] -0.882417062  0.147974647  5.622042135
## [145,]  0.957946569  0.043934783 -0.571936414
## [146,]  0.631153027 -1.615461736 -0.458493584
## [147,]  0.975146229  2.323115378 -0.458493584
## [148,] -0.444278352 -1.902685138  0.902820383
## [149,] -1.046266456  1.058913102 -0.050099394
## [150,]  0.881000722 -1.508015025 -0.503870716
## [151,]  0.861990571  0.411612186 -0.526559282
## [152,] -0.693220801  1.837770723 -0.208919356
## [153,]  0.855653854 -0.108849198 -0.617313546
## [154,] -1.609329013  1.783785303 -0.186230790
## [155,]  0.677320536 -0.687751111 -0.594624980
## [156,] -0.963889136  0.829606098  5.054827982
## [157,]  0.830306986 -0.189827329 -0.594624980
## [158,]  0.504418690 -1.363879193  0.040654871
## [159,] -0.345606618 -0.476788666 -0.163542224
## [160,]  0.773276535  0.634629627 -0.594624980
## [161,]  0.795907666 -0.765584460 -0.208919356
## [162,] -1.567687731  0.773000026 -0.004722261
## [163,]  0.124215677  0.263807443 -0.662690679
## [164,] -1.497078600 -0.701378498  1.197771743
## [165,] -0.761114196  0.245462882  0.176786268
## [166,]  0.727109026 -0.898451490  0.562491892
## [167,] -0.213440809  0.977934972 -0.526559282
## [168,]  0.719867064 -0.286005238 -0.640002113
## [169,] -0.638906085 -1.459795038 -0.435805018
## [170,]  0.700856913 -0.198999609 -0.458493584
## [171,]  0.594037971  0.184401703 -0.617313546
## [172,]  0.346906013 -0.083428879 -0.435805018
## [173,]  0.669173329 -1.959029145 -0.050099394
## [174,]  0.657405140  0.461928695 -0.617313546
## [175,]  0.639300235  0.135133455 -0.549247848
## [176,] -0.385437410 -0.874079431 -0.004722261
## [177,]  0.625721556  0.450921959 -0.526559282
## [178,]  0.599469443  1.843798222 -0.594624980
## [179,]  0.593132726 -0.650799925 -0.322362187
## [180,]  0.584080273 -0.782356629  0.335606230
## [181,]  0.577743556 -0.590787006 -0.413116451
## [182,]  0.576838311  0.593747464 -0.594624980
## [183,]  0.292591297 -1.003277549 -0.617313546
## [184,] -0.854354459 -1.257218678  1.764985896
## [185,]  0.551491444 -0.227826776 -0.549247848
## [186,] -0.309396807  1.923466027 -0.276985055
## [187,] -1.072518569 -1.430967871  2.445642879
## [188,]  0.374968616  1.137270582 -0.571936414
## [189,]  0.487219030 -0.009002377 -0.435805018
## [190,]  0.530670802  0.362343938 -0.208919356
## [191,] -1.540530373  0.186760289  4.827942321
## [192,]  0.475450841  0.189380941 -0.526559282
## [193,] -1.014582871  0.577237360  1.719608763
## [194,]  0.480882313 -0.607297110 -0.390427885
## [195,]  0.373158126 -2.027952279  0.108720569
## [196,]  0.403031220  0.602919745 -0.413116451
## [197,]  0.404841710 -0.585283638 -0.594624980
## [198,]  0.397599748 -1.430443741 -0.571936414
## [199,] -0.294912883 -0.959512669 -0.254296489
## [200,]  0.362295182 -1.771652564  0.335606230
## [201,]  0.340569296 -0.923609744 -0.594624980
## [202,]  0.320653900  1.043189194 -0.617313546
## [203,]  0.274486391 -1.309893773 -0.617313546
## [204,]  0.155899261  1.133077539 -0.571936414
## [205,]  0.147752054 -0.168862117 -0.163542224
## [206,]  0.305264731  1.076733533 -0.594624980
## [207,]  0.229224128  2.108746086 -0.594624980
## [208,] -0.917721628  1.010168985  0.154097701
## [209,]  0.269960165  0.180208660 -0.571936414
## [210,]  0.277202127 -0.093649420 -0.526559282
## [211,]  0.250950014  0.207987566 -0.435805018
## [212,]  0.233750354 -0.332914900 -0.458493584
## [213,]  0.235560845 -0.051456931 -0.617313546
## [214,] -0.119295301  1.780902586 -0.526559282
## [215,] -0.758398460 -0.916271920  0.607869024
## [216,]  0.226508392  0.806282300 -0.617313546
## [217,] -1.020919588 -1.403451031  1.855740160
## [218,]  0.228318883 -1.545490341 -0.617313546
## [219,]  0.216550694 -0.768467176 -0.367739319
## [220,] -0.618085444  1.233710556  0.131409135
## [221,]  0.119689450  0.563609972 -0.526559282
## [222,] -1.195631925 -0.428568679  0.290229098
## [223,] -1.433711431  1.005189747  2.468331445
## [224,]  0.024638697 -1.160254573 -0.503870716
## [225,]  0.094342583 -1.145840990 -0.571936414
## [226,]  0.054511791  0.569899536 -0.617313546
## [227,] -0.831723327  0.785841218 -0.208919356
## [228,]  0.044554093 -1.322734965 -0.571936414
## [229,] -1.421037997  1.117353631  0.290229098
## [230,]  0.055417036 -1.544180015  0.017966305
## [231,] -0.681452613  0.726876560  0.040654871
## [232,]  0.030070169 -0.053553452 -0.640002113
## [233,]  0.012870509 -0.771087828 -0.526559282
## [234,] -0.128347753 -1.566193488 -0.526559282
## [235,] -0.032391755 -1.680715958  2.218757218
## [236,] -0.008855378 -1.183054241 -0.390427885
## [237,] -0.032391755  0.863412502 -0.367739319
## [238,] -0.039633717  1.499968747 -0.594624980
## [239,] -1.107823134 -0.378252171 -0.549247848
## [240,] -0.078559263  0.510934877 -0.640002113
## [241,] -0.978373061 -0.896617034  0.267540532
## [242,] -0.097569414  1.323860967 -0.299673621
## [243,] -0.894185251  1.602960350  1.084328912
## [244,] -0.206198846 -0.565890817  0.154097701
## [245,] -0.115674319 -0.475740406 -0.594624980
## [246,] -0.437036390 -1.702991495 -0.254296489
## [247,] -1.073423814 -0.911292682  0.653246156
## [248,] -0.120200546  0.447515112 -0.549247848
## [249,] -0.122011036 -0.264253831 -0.435805018
## [250,] -0.134684470 -0.952436910 -0.594624980
## [251,] -0.417120994  0.596630181 -0.617313546
## [252,] -0.133779225  1.696255543 -0.481182150
## [253,] -0.138305451  2.651220943  0.880131817
## [254,] -1.068897588 -1.041801126 -0.027410827
## [255,] -0.165462809 -1.477091337 -0.571936414
## [256,] -0.196241148 -1.847127327 -0.481182150
## [257,] -0.274997487  0.720062866 -0.617313546
## [258,] -0.195335903  0.071451624 -0.481182150
## [259,] -0.274092242  0.883329453 -0.027410827
## [260,] -0.236977186  2.276467781  0.539803325
## [261,] -0.217967035  0.155312471 -0.004722261
## [262,] -0.227924733 -0.761391417 -0.095476526
## [263,] -0.558339256 -1.518759696 -0.481182150
## [264,] -0.340175146 -0.631931234  0.040654871
## [265,] -0.754777479 -0.417299878  0.063343437
## [266,] -0.274997487  0.532948350 -0.617313546
## [267,] -1.574024448  1.930279720  0.766688987
## [268,] -0.657916236  0.445418590  0.721311854
## [269,] -0.502214049 -0.541518758  0.086032003
## [270,] -0.316638769 -2.341644261 -0.503870716
## [271,] -0.315733524 -0.025774546 -0.503870716
## [272,] -0.355564316 -1.181481850 -0.617313546
## [273,] -0.325691222 -0.317977187 -0.231607923
## [274,] -0.426173447 -0.264253831 -0.367739319
## [275,] -0.514887483 -1.189343805 -0.231607923
## [276,] -0.395395108 -0.060629211 -0.503870716
## [277,] -0.437941635 -1.498318614 -0.503870716
## [278,] -0.450615069 -1.745708114  0.539803325
## [279,] -0.438846880  0.518010636 -0.617313546
## [280,] -0.457857031 -0.439051285 -0.367739319
## [281,] -1.698948295  1.449390173  3.330496958
## [282,] -0.418931485 -1.607861846 -0.435805018
## [283,] -0.803660724  1.160070250 -0.481182150
## [284,] -0.452425560 -0.208433955 -0.435805018
## [285,] -0.467814729 -0.420444659 -0.549247848
## [286,] -0.461478012 -1.137979035 -0.276985055
## [287,] -0.618990689  0.756751987  0.721311854
## [288,] -0.770166649 -0.172793094  1.243148875
## [289,] -1.012772381  1.611084370  0.176786268
## [290,] -0.502214049  1.459086584 -0.413116451
## [291,] -0.920437364 -0.941954305 -0.004722261
## [292,] -0.532992388 -0.047525954  1.220460309
## [293,] -0.541139596  0.617857458  1.197771743
## [294,] -0.557434011  0.937576939  0.766688987
## [295,] -0.942163250 -1.473160360 -0.186230790
## [296,] -0.540234351 -1.853154825 -0.549247848
## [297,] -1.253567622  0.502024662 -0.458493584
## [298,] -0.559244501  0.189905071 -0.481182150
## [299,] -0.565581218 -1.210833147 -0.186230790
## [300,] -1.548677580  0.711152651  0.449049061
## [301,] -0.563770728 -0.529463761 -0.503870716
## [302,] -0.585496614 -1.238874118 -0.571936414
## [303,] -0.604506765  0.949369871 -0.503870716
## [304,] -0.622611670 -1.455601995 -0.617313546
## [305,] -0.635285104 -0.734660772 -0.072787960
## [306,] -0.635285104  0.540810304 -0.594624980
## [307,] -0.695936537 -1.930464044 -0.549247848
## [308,] -0.692315556  0.999424314 -0.640002113
## [309,] -0.836249554  0.723469713 -0.640002113
## [310,] -0.886038043  1.109491676 -0.345050753
## [311,] -0.976562570 -1.219743362 -0.276985055
## [312,] -1.022730079 -1.683598674  0.721311854
## [313,]  1.941042931  0.886212170 -0.571936414
## [314,]  1.487515052  1.364743131 -0.413116451
## [315,]  0.838454194  0.312027430 -0.571936414
## [316,]  0.138699601  2.322067117 -0.571936414
## [317,]  1.006829814  1.077781793 -0.549247848
## [318,] -0.215251299 -0.740950336 -0.571936414
## [319,] -1.698948295 -0.453726933  0.403671929
## [320,]  0.439241030 -0.645296557 -0.640002113
## [321,] -0.676926386  0.981865949 -0.435805018
## [322,]  0.084384885  1.269089351 -0.481182150
## [323,]  1.453115731 -1.028173738 -0.594624980
## [324,]  1.403327242  1.173435572 -0.594624980
## [325,]  2.604587713 -1.602358478 -0.322362187
## [326,] -0.617180198  0.120457807 -0.390427885
## [327,]  2.078640212 -0.166765596 -0.458493584
## [328,]  0.174909412  0.312027430 -0.503870716
## [329,] -0.412594768  1.173435572 -0.571936414
## [330,] -0.155505111  0.312027430  0.063343437
## [331,] -1.650970295 -0.453726933 -0.027410827
## [332,] -1.025445815  0.216111586  2.128002953
## [333,] -0.361901033  0.503334988 -0.095476526
## [334,] -1.314219055  0.503334988  0.880131817
## [335,]  1.456736713  0.407681209 -0.594624980
## [336,]  0.649257933  1.364743131 -0.254296489
## [337,]  0.333327334  0.503334988 -0.322362187
## [338,] -1.020014343 -0.358073154  2.899414201
## [339,]  1.425053128  0.886212170 -0.594624980
## [340,]  1.427768864  0.216111586  0.494426193
## [341,] -1.635581126  0.312027430  1.311214573
## [342,]  1.188784113 -0.070849752 -0.526559282
## [343,] -1.170285058 -0.262419375  1.787674462
## [344,]  1.121795963 -1.411050920  1.288526007
## [345,]  0.998682606 -0.262419375 -0.617313546
## [346,] -1.230031245  1.843536156 -0.594624980
## [347,]  0.809486345  0.024804027  0.040654871
## [348,]  0.829401741  0.120457807 -0.526559282
## [349,]  0.848411892  0.312027430 -0.413116451
## [350,] -1.136790983 -0.262419375 -0.254296489
## [351,] -1.077950040  1.460658975  2.672528540
## [352,]  0.722582799  0.216111586 -0.594624980
## [353,]  0.706288385  1.077781793 -0.435805018
## [354,] -0.839870535  0.790558391 -0.435805018
## [355,]  0.380400088 -1.123827517 -0.367739319
## [356,] -0.273186996  1.556312754 -0.231607923
## [357,]  0.584080273  0.694904612 -0.050099394
## [358,]  0.560543896  1.269089351 -0.549247848
## [359,]  0.557828161 -0.453726933 -0.549247848
## [360,] -1.031782532  1.269089351 -0.322362187
## [361,] -1.177527020 -0.932519959  0.517114759
## [362,]  0.316127674 -0.166765596  3.353185524
## [363,]  0.299833259 -0.645296557 -0.594624980
## [364,] -1.411080299  0.790558391 -0.118165092
## [365,]  0.006533792  1.173435572 -0.526559282
## [366,] -1.510657279  0.981865949 -0.435805018
## [367,]  0.257286731  1.269089351 -0.481182150
## [368,] -1.697137804 -0.166765596  2.400265747
## [369,] -0.652484764 -0.836604115  0.267540532
## [370,] -0.744819781 -0.070849752  2.899414201
## [371,] -1.293398414  0.024804027  0.925508949
## [372,]  0.209308732 -1.315397141 -0.594624980
## [373,]  0.167667450  1.077781793 -0.571936414
## [374,]  0.056322282  0.024804027 -0.571936414
## [375,] -0.516697974  0.120457807 -0.345050753
## [376,] -0.398110843 -0.645296557  1.424657404
## [377,]  0.062658998 -1.698274322 -0.231607923
## [378,] -0.678736877  0.886212170 -0.322362187
## [379,] -1.195631925  1.173435572  0.017966305
## [380,] -0.174515262 -1.698274322 -0.072787960
## [381,] -0.017002585 -0.932519959 -0.345050753
## [382,] -1.536004146  0.024804027  2.445642879
## [383,] -0.810902686 -1.315397141 -0.549247848
## [384,] -0.252366355  0.790558391 -0.435805018
## [385,] -0.255987336  0.407681209 -0.571936414
## [386,] -0.238787676  0.312027430 -0.345050753
## [387,] -0.239692921 -0.166765596  2.354888615
## [388,] -0.323880731 -1.028173738 -0.526559282
## [389,] -0.304870581  1.556312754 -0.571936414
## [390,] -0.451520314  1.651966533 -0.050099394
## [391,] -0.429794428 -0.932519959 -0.458493584
## [392,] -0.497687823  1.747620312 -0.640002113
## [393,] -1.561351014  0.120457807 -0.571936414
## [394,] -0.498593068  0.598988767 -0.276985055
## [395,] -0.532992388 -1.411050920 -0.413116451
## [396,] -0.520318955 -0.070849752 -0.367739319
## [397,] -0.533897634  1.269089351 -0.617313546
## [398,] -0.491351106  1.077781793  0.925508949
## [399,] -0.595454312 -0.836604115  1.107017478
## [400,] -0.628948387 -0.645296557 -0.617313546
## [401,] -0.889659024  1.747620312  0.222163400
## [402,] -0.882417062  0.120457807 -0.549247848
## [403,] -0.703178499  1.460658975 -0.163542224
## [404,] -0.747535517 -1.028173738  0.312917664
## [405,] -0.695031292  0.120457807 -0.503870716
## [406,] -1.099675927 -0.453726933  0.290229098
## [407,] -0.714041442  0.312027430 -0.481182150
## [408,] -0.752966989  0.024804027 -0.299673621
## [409,] -0.770166649 -0.740950336 -0.571936414
## [410,] -0.765640423 -1.123827517 -0.390427885
## [411,] -0.723093895  0.024804027 -0.594624980
## [412,] -0.743009291  1.556312754 -0.503870716
## [413,] -0.840775780 -1.506704699 -0.571936414
## [414,] -1.119591323  1.556312754 -0.458493584
## [415,] -0.737577819 -1.123827517 -0.526559282
## [416,] -0.781029592  0.598988767 -0.367739319
## [417,] -1.110538870  0.694904612 -0.549247848
## [418,] -0.852543968  0.216111586 -0.571936414

Other examples
Create a matrix

X <-  sample(0:200, 100)
Mat <- matrix(X, 50, 50) 

Obtain the mean value of each row for matrix Mat

apply(Mat, 1, mean)
##  [1]  57.0 119.0  45.5  79.5 123.0 163.5  17.0 108.5 188.5  90.5 137.0 111.5
## [13]  79.5 108.0 107.0 118.5  80.5 149.5  77.5 113.0 127.0 129.5  34.5 120.0
## [25]  83.0 150.0  93.0  80.0 121.0  80.0  73.0  30.0  95.0 149.5  81.5 108.0
## [37] 154.5  84.5  85.0  83.5  75.0  73.5 113.0  58.0 125.5  96.5  90.0  57.0
## [49]  48.0 142.0

Obtain the mean value of each column for matrix Mat

apply(Mat, 2, mean)
##  [1]  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32
## [11]  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32
## [21]  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32
## [31]  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32
## [41]  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32  96.28 100.32

Calculate the sum of each column for matrix Mat

apply(Mat, 2, sum)
##  [1] 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814
## [16] 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016
## [31] 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814
## [46] 5016 4814 5016 4814 5016

Calculate the sum of each row for matrix Mat

apply(Mat, 1, sum)
##  [1] 2850 5950 2275 3975 6150 8175  850 5425 9425 4525 6850 5575 3975 5400 5350
## [16] 5925 4025 7475 3875 5650 6350 6475 1725 6000 4150 7500 4650 4000 6050 4000
## [31] 3650 1500 4750 7475 4075 5400 7725 4225 4250 4175 3750 3675 5650 2900 6275
## [46] 4825 4500 2850 2400 7100

There is no one way of doing things in R!

colSums(Mat)
##  [1] 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814
## [16] 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016
## [31] 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814 5016 4814
## [46] 5016 4814 5016 4814 5016
rowSums(Mat)
##  [1] 2850 5950 2275 3975 6150 8175  850 5425 9425 4525 6850 5575 3975 5400 5350
## [16] 5925 4025 7475 3875 5650 6350 6475 1725 6000 4150 7500 4650 4000 6050 4000
## [31] 3650 1500 4750 7475 4075 5400 7725 4225 4250 4175 3750 3675 5650 2900 6275
## [46] 4825 4500 2850 2400 7100

lapply

Check which variables of the pbc data set are numeric

lapply(pbc, is.numeric)
## $id
## [1] TRUE
## 
## $time
## [1] TRUE
## 
## $status
## [1] TRUE
## 
## $trt
## [1] TRUE
## 
## $age
## [1] TRUE
## 
## $sex
## [1] FALSE
## 
## $ascites
## [1] TRUE
## 
## $hepato
## [1] TRUE
## 
## $spiders
## [1] TRUE
## 
## $edema
## [1] TRUE
## 
## $bili
## [1] TRUE
## 
## $chol
## [1] TRUE
## 
## $albumin
## [1] TRUE
## 
## $copper
## [1] TRUE
## 
## $alk.phos
## [1] TRUE
## 
## $ast
## [1] TRUE
## 
## $trig
## [1] TRUE
## 
## $platelet
## [1] TRUE
## 
## $protime
## [1] TRUE
## 
## $stage
## [1] TRUE

Obtain the number of missing values per pbc variable

lapply(pbc, function(x) { sum(is.na(x)) } ) 
## $id
## [1] 0
## 
## $time
## [1] 0
## 
## $status
## [1] 0
## 
## $trt
## [1] 106
## 
## $age
## [1] 0
## 
## $sex
## [1] 0
## 
## $ascites
## [1] 106
## 
## $hepato
## [1] 106
## 
## $spiders
## [1] 106
## 
## $edema
## [1] 0
## 
## $bili
## [1] 0
## 
## $chol
## [1] 134
## 
## $albumin
## [1] 0
## 
## $copper
## [1] 108
## 
## $alk.phos
## [1] 106
## 
## $ast
## [1] 106
## 
## $trig
## [1] 136
## 
## $platelet
## [1] 11
## 
## $protime
## [1] 2
## 
## $stage
## [1] 6

Other examples
Obtain the quadratic term of the vector 1:3
Present the results as a list

lapply(1:3, function(x) { x^2 } )
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 4
## 
## [[3]]
## [1] 9

Create a list

A <- matrix(1:9, 3,3)
B <- matrix(4:15, 4,3)
C <- matrix(8:10, 3,2)

Select elements in a list

MyList <- list(A, B, C) 

Select the first row of each element
Present the results as a list

lapply(MyList,"[", 1, )
## [[1]]
## [1] 1 4 7
## 
## [[2]]
## [1]  4  8 12
## 
## [[3]]
## [1] 8 8

Select the second column of each element
Present the results as a list

lapply(MyList,"[", , 2)
## [[1]]
## [1] 4 5 6
## 
## [[2]]
## [1]  8  9 10 11
## 
## [[3]]
## [1]  8  9 10

sapply

Obtain the number of missing values per pbc variable

sapply(pbc, function(x) { sum(is.na(x)) } ) 
##       id     time   status      trt      age      sex  ascites   hepato 
##        0        0        0      106        0        0      106      106 
##  spiders    edema     bili     chol  albumin   copper alk.phos      ast 
##      106        0        0      134        0      108      106      106 
##     trig platelet  protime    stage 
##      136       11        2        6

Other examples
Obtain the quadratic term of the vector 1:3
Present the results as a vector

sapply(1:3, function(x) { x^2 } )
## [1] 1 4 9

Note that you can change the output of the sapply format and make it identical to the lapply output by specifying simplify = FALSE

sapply(1:3, function(x) { x^2 }, simplify = FALSE)
## [[1]]
## [1] 1
## 
## [[2]]
## [1] 4
## 
## [[3]]
## [1] 9

tapply

Obtain the mean age and time per sex

tapply(pbc$age, pbc$sex, mean)
##        m        f 
## 55.71072 50.15694
tapply(pbc$time, pbc$sex, mean)
##        m        f 
## 1894.023 1920.578

Obtain the mean age and time (both elements of the variables divided by two) per sex

tapply(pbc$age, pbc$sex, function(x) { mean(x/2) } )
##        m        f 
## 27.85536 25.07847
tapply(pbc$time, pbc$sex, function(x) { mean(x/2) } )
##        m        f 
## 947.0114 960.2888

Obtain the mean age and time per sex and status

tapply(pbc$age, list(pbc$status, pbc$sex), mean)
##          m        f
## 0 55.68595 49.02203
## 1 42.16290 41.62939
## 2 57.42174 53.30739
tapply(pbc$time, list(pbc$status, pbc$sex), mean)
##          m        f
## 0 2462.176 2322.953
## 1 1147.667 1600.545
## 2 1584.875 1340.504

mapply

Create a list:

  • 1st element: repeats 1 four times
  • 2nd element: repeats 2 three times  
  • 3rd element: repeats 3 two times  
  • 4th element: repeats 4 one time
mapply(rep, x = 1:4, times = 4:1)
## [[1]]
## [1] 1 1 1 1
## 
## [[2]]
## [1] 2 2 2
## 
## [[3]]
## [1] 3 3
## 
## [[4]]
## [1] 4
#### alternative run: list(rep(1, 4), rep(2, 3), rep(3, 2), rep(4, 1))

Create a list:

  • 1st element: repeats 4 one time
  • 2nd element: repeats 4 two times  
  • 3rd element: repeats 4 three times  
  • 4th element: repeats 4 four times
mapply(rep, times = 1:4, x = 4)
## [[1]]
## [1] 4
## 
## [[2]]
## [1] 4 4
## 
## [[3]]
## [1] 4 4 4
## 
## [[4]]
## [1] 4 4 4 4
#### alternative run: list(rep(4, times = 1), rep(4, times = 2), rep(4, times = 3), rep(4, times = 4))

Create a list:

  • 1st element: repeats 1 four times  
  • 2nd element: repeats 2 four times  
  • 3rd element: repeats 3 four times  
  • 4th element: repeats 4 four times
mapply(rep, x = 1:4, times = 4, SIMPLIFY = FALSE)
## [[1]]
## [1] 1 1 1 1
## 
## [[2]]
## [1] 2 2 2 2
## 
## [[3]]
## [1] 3 3 3 3
## 
## [[4]]
## [1] 4 4 4 4
### alternative run: list(rep(1, 4), rep(2, 4), rep(3, 4), rep(4, 4))

Note: if the length is the same we can obtain a simplified output

mapply(rep, x = 1:4, times = 4, SIMPLIFY = TRUE)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    1    2    3    4
## [3,]    1    2    3    4
## [4,]    1    2    3    4
### alternative run: matrix(c(rep(1, 4), rep(2, 4), rep(3, 4), rep(4, 4)), 4, 4)

Other examples

mapply(function(x,y) { seq_len(x) + y },
       c(a = 1, b = 2, c = 3),  
       c(A = 10, B = 0, C = -10))
## $a
## [1] 11
## 
## $b
## [1] 1 2
## 
## $c
## [1] -9 -8 -7
#### alternative run: list(c(1) + 10, c(1, 2) + 0, c(1, 2, 3) - 10)

Note!

mapply(mean, MyList)
## [1] 5.0 9.5 9.0
sapply(MyList, mean)
## [1] 5.0 9.5 9.0

Longitudinal data

Let’s assume that only the long format data set pbcseq is available
We want to obtain the mean serum bilirubin of the last follow-up measurement (specified as day) per status group
Each patient is counted once!

head(pbcseq)

Sort data

pbcseq <- pbcseq[order(pbcseq$id, pbcseq$day), ]

Select the last follow-up measurement of each patient

pbcseq.idNEW2 <- pbcseq[tapply(rownames(pbcseq), pbcseq$id, tail,  1), ]

Step by step

tapply(rownames(pbcseq), pbcseq$id, tail,  1)
##      1      2      3      4      5      6      7      8      9     10     11 
##    "2"   "11"   "15"   "22"   "28"   "34"   "41"   "49"   "56"   "57"   "69" 
##     12     13     14     15     16     17     18     19     20     21     22 
##   "71"   "83"   "90"  "101"  "114"  "117"  "118"  "133"  "137"  "149"  "152" 
##     23     24     25     26     27     28     29     30     31     32     33 
##  "154"  "167"  "179"  "185"  "186"  "189"  "199"  "202"  "214"  "230"  "240" 
##     34     35     36     37     38     39     40     41     42     43     44 
##  "254"  "258"  "269"  "271"  "281"  "289"  "304"  "308"  "324"  "339"  "350" 
##     45     46     47     48     49     50     51     52     53     54     55 
##  "359"  "367"  "374"  "383"  "387"  "396"  "406"  "415"  "418"  "423"  "429" 
##     56     57     58     59     60     61     62     63     64     65     66 
##  "435"  "446"  "462"  "469"  "482"  "496"  "506"  "509"  "515"  "521"  "534" 
##     67     68     69     70     71     72     73     74     75     76     77 
##  "544"  "558"  "562"  "575"  "586"  "592"  "607"  "611"  "616"  "617"  "619" 
##     78     79     80     81     82     83     84     85     86     87     88 
##  "623"  "634"  "638"  "648"  "658"  "673"  "676"  "687"  "688"  "690"  "694" 
##     89     90     91     92     93     94     95     96     97     98     99 
##  "698"  "707"  "710"  "711"  "726"  "729"  "730"  "744"  "748"  "762"  "775" 
##    100    101    102    103    104    105    106    107    108    109    110 
##  "779"  "791"  "803"  "804"  "814"  "824"  "829"  "840"  "848"  "858"  "865" 
##    111    112    113    114    115    116    117    118    119    120    121 
##  "873"  "884"  "889"  "900"  "912"  "923"  "928"  "935"  "937"  "945"  "946" 
##    122    123    124    125    126    127    128    129    130    131    132 
##  "956"  "958"  "959"  "968"  "973"  "984"  "989" "1001" "1007" "1011" "1017" 
##    133    134    135    136    137    138    139    140    141    142    143 
## "1026" "1038" "1048" "1057" "1069" "1075" "1081" "1093" "1096" "1105" "1108" 
##    144    145    146    147    148    149    150    151    152    153    154 
## "1112" "1116" "1123" "1128" "1134" "1137" "1146" "1157" "1160" "1170" "1171" 
##    155    156    157    158    159    160    161    162    163    164    165 
## "1174" "1178" "1189" "1198" "1202" "1210" "1221" "1222" "1225" "1226" "1231" 
##    166    167    168    169    170    171    172    173    174    175    176 
## "1239" "1245" "1255" "1260" "1261" "1271" "1278" "1287" "1297" "1307" "1313" 
##    177    178    179    180    181    182    183    184    185    186    187 
## "1314" "1320" "1323" "1333" "1334" "1339" "1347" "1352" "1357" "1361" "1365" 
##    188    189    190    191    192    193    194    195    196    197    198 
## "1373" "1378" "1382" "1383" "1393" "1397" "1402" "1403" "1411" "1420" "1428" 
##    199    200    201    202    203    204    205    206    207    208    209 
## "1431" "1442" "1452" "1454" "1463" "1471" "1476" "1484" "1491" "1494" "1496" 
##    210    211    212    213    214    215    216    217    218    219    220 
## "1506" "1514" "1520" "1522" "1526" "1530" "1537" "1541" "1551" "1557" "1562" 
##    221    222    223    224    225    226    227    228    229    230    231 
## "1570" "1574" "1575" "1583" "1592" "1601" "1605" "1609" "1612" "1616" "1620" 
##    232    233    234    235    236    237    238    239    240    241    242 
## "1624" "1625" "1627" "1629" "1631" "1633" "1636" "1639" "1647" "1651" "1659" 
##    243    244    245    246    247    248    249    250    251    252    253 
## "1661" "1665" "1667" "1673" "1677" "1685" "1688" "1690" "1691" "1694" "1702" 
##    254    255    256    257    258    259    260    261    262    263    264 
## "1706" "1712" "1718" "1721" "1729" "1734" "1735" "1741" "1744" "1749" "1753" 
##    265    266    267    268    269    270    271    272    273    274    275 
## "1757" "1764" "1765" "1767" "1774" "1777" "1785" "1791" "1794" "1799" "1803" 
##    276    277    278    279    280    281    282    283    284    285    286 
## "1805" "1810" "1815" "1822" "1829" "1830" "1834" "1838" "1845" "1846" "1848" 
##    287    288    289    290    291    292    293    294    295    296    297 
## "1852" "1854" "1858" "1865" "1870" "1873" "1875" "1882" "1884" "1887" "1890" 
##    298    299    300    301    302    303    304    305    306    307    308 
## "1893" "1894" "1896" "1899" "1904" "1908" "1909" "1914" "1916" "1921" "1926" 
##    309    310    311    312 
## "1931" "1936" "1940" "1945"
### alternative run: pbcseq.idNEW2 <- pbcseq[!duplicated(pbcseq[c("id")], fromLast = TRUE), ]

Obtain the mean serum bilirubin per status group

tapply(pbcseq.idNEW2$bili, pbcseq.idNEW2$status, mean)
##         0         1         2 
##  2.190909  9.172414 11.800000